package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.log.annotation.Log;
import com.atguigu.spzx.common.log.enums.OperatorType;
import com.atguigu.spzx.manager.service.ProductServcie;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product/product")
@Tag(name = "商品管理模块")
public class ProductController {
    @Autowired
    private ProductServcie productServcie;


    @GetMapping("/{current}/{size}")
    @Operation(summary = "分页带条件查询商品信息")
    public Result<PageInfo<Product>> getProductByPage(@PathVariable Integer current,
                                                      @PathVariable Integer size,
                                                      ProductDto productDto){
        PageInfo<Product> productList=productServcie.getProductByPage(current,size,productDto);
     return Result.build(productList, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("save")
    @Operation(summary = "添加商品信息")
    public Result save(@RequestBody Product product){
        productServcie.save(product);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }
    @GetMapping("getById/{id}")
    @Operation(summary = "根据id查询商品信息")
    public Result<Product> getById(@PathVariable Long id){
        Product product=productServcie.getById(id);
        return Result.build(product,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("updateById")
    @Operation(summary = "根据id修改商品信息")
    public Result updateById(@RequestBody Product product){
        productServcie.updateById(product);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("修改成功");
    }
    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "根据id删除商品信息")
    public Result deleteById(@PathVariable Long id){
        productServcie.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("删除成功");
    }

    @GetMapping("updateAuditStatus/{id}/{auditStatus}")
    @Operation(summary = "审批商品")
    public Result updateAuditStatus(@PathVariable Long id,
                                    @PathVariable Integer auditStatus){
        productServcie.updateAuditStatus(id,auditStatus);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @Log(title = "上下架商品",operatorType = OperatorType.MANAGE)
    @GetMapping("updateStatus/{id}/{status}")
    @Operation(summary = "上下架商品")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status){
        productServcie.updateStatus(id,status);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
